#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e5 + 5;
const int MX = 1e5;

ll c[5], d[5], s;
ll dp[N];
void init(int n) {
  dp[0] = 1;
  rep(i, 0, 3) {
    rep(j, c[i], n) { dp[j] += dp[j - c[i]]; }
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  rep(i, 0, 3) cin >> c[i];
  init(MX);
  int t;
  cin >> t;
  while (t--) {
    rep(i, 0, 3) cin >> d[i];
    cin >> s;
    ll ans = 0;
    rep(i, 0, 15) {
      ll sum = 0;
      ll sign = 1;
      rep(j, 0, 3) {
        if (i & (1 << j)) {
          sum += (d[j] + 1) * c[j];
          sign *= -1;
        }
      }
      if (sum <= s) ans += sign * dp[s - sum];
    }
    cout << ans << endl;
  }
  return 0;
}